package org.aeon.gamechatnest.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aeon.gamechatnest.common.response.Result;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.exception.TargetIsAnEmptyException;
import org.aeon.gamechatnest.pojo.TextChannel;
import org.aeon.gamechatnest.service.TextChannelService;
import org.aeon.gamechatnest.validated.Add;
import org.aeon.gamechatnest.validated.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Liys 2024/5/11 11:08
 */
@Validated
@RestController
@RequestMapping("/text/channel")
@Tag(name = "TextChannelControllerApi", description = "文字频道控制器")
public class TextChannelController {
    @Autowired
    private TextChannelService textChannelService;

    @PreAuthorize("hasAnyAuthority('channel:add','admin','creator') AND " +
            "#textChannel.serverId == T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "新增文字频道")
    @PostMapping
    public Result save(@RequestBody @Validated(Add.class) TextChannel textChannel) {
        boolean result = textChannelService.save(textChannel);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('channel:del','admin','creator') AND " +
            "@textChannelController.getUserServerId(#tcId) == " +
            "T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "删除文字频道",
            parameters = {@Parameter(name = "tcId", description = "频道编号")})
    @DeleteMapping("/{tcId}")
    public Result removeByTcId(@PathVariable("tcId") Long tcId) {
        boolean result = textChannelService.removeByTcId(tcId);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('channel:modify','admin','creator') AND " +
            "@textChannelController.getUserServerId(#textChannel.tcId) == " +
            "T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "修改文字频道")
    @PutMapping
    public Result modifyByTcId(@RequestBody @Validated(Update.class) TextChannel textChannel) {
        boolean result = textChannelService.modifyByTcId(textChannel);
        return ResultBuild.successIf(result).build();
    }

    @PreAuthorize("hasAnyAuthority('channel:get','admin','creator')")
    @Operation(summary = "查询服务器所有文字频道")
    @GetMapping
    public Result textChannels() {
        List<TextChannel> result = textChannelService.textChannels();
        return ResultBuild.result().successful().data(result).build();
    }

    @PreAuthorize("hasAnyAuthority('channel:modify','admin','creator') AND " +
            "@textChannelController.getUserServerId(#tcId) == " +
            "T(org.aeon.gamechatnest.util.SecurityUtil).server")
    @Operation(summary = "根据id查询文字频道",
            parameters = {@Parameter(name = "tcId", description = "频道id")})
    @GetMapping("/{tcId}")
    public Result textChannel(@PathVariable("tcId") Long tcId) {
        TextChannel result = textChannelService.findByTcId(tcId);
        return ResultBuild.result().successful().data(result).build();
    }


    @Operation(
            summary = "查询文字频道",
            parameters = {@Parameter(name = "name", description = "频道名称")}
    )
    @GetMapping("/search")
    public Result findByName(String name) {
        List<TextChannel> result = textChannelService.findByName(name);
        return ResultBuild.result().successful().data(result).build();
    }


    /**
     * 获取频道所在的服务器id
     * 调用后可以无需判断 roleId 是否存在
     *
     * @param tcId 频道id
     * @return 频道的服务器id
     * @throws TargetIsAnEmptyException 如果频道不存在
     */
    public Long getUserServerId(Long tcId) throws TargetIsAnEmptyException {
        return textChannelService.getServerIdByChannelId(tcId);
    }
}
